package com.lysj.bhp.util.random;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FormatUtil {

	public static Date stringTodates(String string) throws Exception {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return format.parse(string);
	}

	/**
	 * 输入:时间(Date) 输出格式:yyyy-MM-dd HH:mm:ss
	 */
	public static String dateToString(Date date) throws Exception {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}

	/**
	 * 输入:时间(Date) 输出格式: yyyy-MM-dd
	 */
	public static String dateToString2(Date date) throws Exception {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(date);
	}

	/**
	 * 输入:时间(Date) 输出格式:HH:mm:ss
	 */
	public static String dateToString3(Date date) {
		DateFormat format = new SimpleDateFormat("HH:mm:ss");
		return format.format(date);
	}

	/**
	 * 将HH:mm字符串格式的时间转化成Date类型
	 * 
	 * @param HHmm
	 * @return Date
	 * @throws Exception
	 */
	public static Date stringToTime(String HHmm) throws Exception {
		DateFormat format = new SimpleDateFormat("HH:mm");
		return format.parse(HHmm);
	}

	/**
	 * 输入格式:yyyy-MM-dd HH:mm:ss 输出:时间(Date)
	 */
	public static Date stringTodate(String string) throws Exception {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.parse(string);
	}

	/**
	 * 输入格式:yyyy-MM-dd 输出:时间(Date)
	 */
	public static Date stringTodate2(String string) throws Exception {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.parse(string);
	}

	/**
	 * 根据Calendar对象中定义的静态变量返回对应的中文名称
	 * 
	 * @param week
	 *            (1~7)
	 * @return
	 */
	public static String getWeekName(int week) {
		switch (week) {
		case Calendar.SUNDAY: // 1
			return "周日";
		case Calendar.MONDAY: // 2
			return "周一";
		case Calendar.TUESDAY: // 3
			return "周二";
		case Calendar.WEDNESDAY: // 4
			return "周三";
		case Calendar.THURSDAY: // 5
			return "周四";
		case Calendar.FRIDAY: // 6
			return "周五";
		case Calendar.SATURDAY: // 7
			return "周六";
		default:
			return "";
		}
	}

	// 输出时间
	public static void showDate(Date date) {
		try {
			System.out.println("FormatUtil.showDate():" + dateToString(date));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 对日期+"23:59:59"
	public static Date getDayEndDay(Date day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(day);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	// 对日期取"0:0:0"
	public static Date getDayStartDay(Date day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(day);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	// 获取今天 "0:0:0"
	public static Date getStartToday() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	// 获取昨天"0:0:0"
	public static Date getStartYesterday() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 判断一个时间是否在两个时间之间
	 * 
	 * @param currentDate
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean isBetweenTime(Date currentDate, Date startDate, Date endDate) {
		if (currentDate == null) {
			return false;
		}
		if (startDate == null && endDate == null) {
			return false;
		}

		if (startDate == null) {
			if (currentDate.getTime() < getDayEndDay(endDate).getTime()) {
				return true;
			}
		}
		if (endDate == null) {
			if (currentDate.getTime() > startDate.getTime()) {
				return true;
			}
		}
		if (currentDate.getTime() > startDate.getTime() && currentDate.getTime() < getDayEndDay(endDate).getTime()) {
			return true;
		}

		return false;
	}

	/**
	 * 在今天加上month个月份
	 * 
	 * @param month
	 * @return
	 */
	public static Date addMonth(int month) {
		Calendar calendar = Calendar.getInstance();
		if (month > 0) {
			calendar.add(Calendar.MONTH, month);
		}
		return calendar.getTime();
	}

	/**
	 * @author zk
	 * @date 2018/1/16 15:22
	 * @Description 某个日期加减count天
	 */
	public static Date addDay(Date date,int count){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR,count);
		return calendar.getTime();
	}


	/**
	 * 现在时间的秒值
	 */
	public static String nowTimeStampSecond() {
		return String.valueOf(System.currentTimeMillis() / 1000);
	}

	/**
	 * 现在时间的秒值
	 */
	public static String nextMonthTimeStampSecond() {
		Date nextMonth = addMonth(1);// 下个月的时间
		return String.valueOf(nextMonth.getTime() / 1000);
	}



}
